package com.beerandjesus.common.io;

import java.util.*;
import java.io.File;
import java.io.FilenameFilter;

public class ImageFilenameFilter implements FilenameFilter {
	private List acceptedFileExtensions = new ArrayList();

	public ImageFilenameFilter() {
		defaultAccpetedExtensions();
	}

    public ImageFilenameFilter(String[] acceptedExtensions) {
        addExtensions(acceptedExtensions);
    }

    private void addExtensions(String[] acceptedExtensions) {
        for (int i = 0; i < acceptedExtensions.length; i++) {
            String acceptedExtension = acceptedExtensions[i];
            acceptedFileExtensions.add(acceptedExtension);
        }
    }

    private void defaultAccpetedExtensions() {
		acceptedFileExtensions.add("jpg");
		acceptedFileExtensions.add("JPG");
		acceptedFileExtensions.add("jpeg");
		acceptedFileExtensions.add("JPEG");
		acceptedFileExtensions.add("gif");
		acceptedFileExtensions.add("GIF");
		acceptedFileExtensions.add("png");
		acceptedFileExtensions.add("PNG");
	}

	public boolean accept(File file, String fname) {
        File f = new File(fname);
        TokenizedFile tokenizedFile = new TokenizedFile(f);
		return (acceptedFileExtensions.contains(tokenizedFile.extension()));
	}
}
